using System;
using System.IO;
using CodePlex.TfsLibrary.ClientEngine;
using CodePlex.TfsLibrary.ObjectModel;
using CodePlex.TfsLibrary.RepositoryWebSvc;

namespace CodePlex.CodePlexClientLibrary
{
    [Command("info", "Display information about an item", UsageSummary = "[path...] [options]")]
    public class InfoCommand : NonAuthenticatedCommand
    {
        bool recursive;

        [Option("recursive", "act recursively on directories", ShortName = "e")]
        public bool Recursive
        {
            get { return recursive; }
            set { recursive = value; }
        }

        public override void Execute()
        {
            if (Arguments.Count == 0)
                Arguments.Add(".");

            string currentDirectory = Directory.GetCurrentDirectory();

            foreach (string path in Arguments)
            {
                try
                {
                    ProcessItem(currentDirectory, Engine.GetSourceItem(path));
                }
                catch (TfsStateException ex)
                {
                    Console.WriteLine("error: {0}", ex.Message);
                    Console.WriteLine();
                }
            }
        }

        void ProcessItem(string basePath,
                         SourceItem item)
        {
            if (File.Exists(item.LocalName) ||
                Directory.Exists(item.LocalName) ||
                Engine.IsFileTracked(item.LocalName) ||
                Engine.IsFolderTracked(item.LocalName))
            {
                Console.WriteLine("Path: {0}", CodePlexClientUtil.GetRelativePath(basePath, item.LocalName));
                Console.WriteLine("Item Type: {0}", item.ItemType);
                Console.WriteLine("Status: {0}", item.LocalItemStatus);

                if (item.LocalItemStatus != SourceItemStatus.Unversioned)
                {
                    string directory = item.ItemType == ItemType.Folder ? item.LocalName : Path.GetDirectoryName(item.LocalName);
                    TfsFolderInfo folderInfo = Engine.GetFolderInfo(directory);

                    Console.WriteLine("TFS Url: {0}", folderInfo.TfsUrl);
                    Console.WriteLine("TFS Project Path: {0}", folderInfo.ServerPath);
                    Console.WriteLine("Item ID: {0}", item.ItemId);
                    Console.WriteLine("Revision: {0}", item.LocalChangesetId);

                    if (item.ItemType == ItemType.File)
                        Console.WriteLine("Last updated date: {0}", File.GetLastWriteTime(item.LocalTextBaseName));
                }
            }
            else
                ConsoleUtil.PrintCommandError(item.LocalName, SourceItemResult.E_PathNotFound);

            Console.WriteLine();

            if (recursive && item.ItemType == ItemType.Folder && item.LocalItemStatus != SourceItemStatus.Unversioned)
                foreach (SourceItem subItem in Engine.GetSourceItems(item.LocalName))
                    ProcessItem(basePath, subItem);
        }
    }
}